<template>
  <div class="login-layout">
    <div class="login-box">
      <div class="bg"></div>
      <div class="box">
        <p class="title">混合云AI算力平台</p>
        <p class="hint">用户登录</p>
        <div class="item">
          <el-input maxlength="10" v-model="addForm.username" placeholder="请输入用户名" />
        </div>
        <div class="item">
          <el-input maxlength="10" v-model="addForm.password" placeholder="请输入密码" type="password" />
        </div>
        <div class="item">
          <el-input maxlength="10" v-model="addForm.captcha_code" placeholder="请输入验证码">
            <div slot="append" class="code-pic">
              <img :src="codePic" alt="" @click="getCode">
            </div>
          </el-input>
        </div>
        <div class="item">
          <a href="javascript:;" class="submit" @click="submit">立即登录</a>
        </div>
      </div>
    </div>
    <!-- <el-form ref="form" :rules="rules" label-position="left" :model="addForm" label-width="120px">
      <el-form-item label="用户名" prop="username">
        <el-input maxlength="10" v-model="addForm.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input maxlength="10" v-model="addForm.password" placeholder="请输入密码" type="password" />
      </el-form-item>
      <el-form-item label="验证码" prop="captcha_code">
        <el-input maxlength="10" v-model="addForm.captcha_code" placeholder="请输入验证码">
          <div slot="append" class="code-pic">
            <img :src="codePic" alt="" @click="getCode">
          </div>
        </el-input>
      </el-form-item>
      <el-form-item label=" ">
        <el-button @click="submit" class="none-type">登录</el-button>
      </el-form-item>

    </el-form> -->
  </div>
</template>

<script>
import { verificationCode, login } from '@/api/login'
import { setCookie } from '@/utils/tool'
import { encrypt } from '@/utils/rsaEncrypt'
export default {
  data () {
    return {
      addForm: {
      },
      codePic: '',
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
        captcha_code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
        ],
      }
    };
  },
  async created () {
    this.getCode()
  },
  mounted () {
    
  },
  destroyed () {
    
  },
  methods: {
    async getCode () {
      let res = await verificationCode()
      this.addForm.captcha_key = res.data.uuid
      this.codePic = res.data.img
    },
    async submit () {
      let data = {
        client_id: 'platform',
        client_secret: encodeURIComponent(encrypt('123456'))
      }
      // if (this.navActive == 0) {

        if (!this.addForm.username) {
          this.$message.error('请输入用户名')
          return false
        }
        if (!this.addForm.password) {
          this.$message.error('请输入密码')
          return false
        }
        if (!this.addForm.captcha_code) {
          this.$message.error('请输入验证码')
          return false
        }
      // }

      data.grant_type = 'password'
      data.password = encodeURIComponent(encrypt(this.addForm.password))
      data.username = this.addForm.username
      //data.captcha_key = this.addForm.captcha_key
      //data.captcha_code = this.addForm.captcha_code
      // this.$refs.form.validate(async (valid) => {
      //   if (valid) {
          let [ err, res ] = await this.$awaitWrap(login({
            ...data
          }))
          if ( err ) {
            this.getCode()
            return
          }
          setCookie('login_ticket', `Bearer ${res.data.access_token}`)
          setCookie('refresh_token', res.data.refresh_token)
          this.$router.push({
            path: '/'
          })
      //   }
      // })
    }
  }
}
</script>
<style lang="less" scoped>
.login-layout{
  width: 100%;
  height: 100%;
  background: url(~@/assets/img/401/login-bg.png) no-repeat;
  background-size: 100% 100%;
  position: relative;
}
.login-box{
  width: 880px;
  height: 552px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, 50%);
  display: flex;
  .bg{
    width: 464px;
    height: 522px;
    background: url(~@/assets/img/401/login-rocket.png) no-repeat #5776d5;
    background-size: 464px 522px;
    overflow: hidden;
    border-radius: 14px 0px  0px 14px;
  }
  .box{
    flex: 1;
    height: 522px;
    padding: 0 60px;
    background: #FFFFFF;
    border-radius: 0px 14px 14px 0px;
    box-shadow: 0px 15px 30px 0px rgba(25, 74, 142, 0.1076);
  }
  .title{
    line-height: 46px;
    color: #3D3D3D;
    font-size: 32px;
    padding-top: 26px;
    text-align: center;
  }
  .hint{
    font-size: 20px;
    color: #333B48;
    line-height: 20px;
    padding: 84px 0 24px 0;
    text-align: center;
  }
  .item{
    padding-top: 10px;
  }
  .submit{
    margin-top: 48px;
    display: block;
    height: 50px;
    background: #4A75D4;
    box-shadow: 0px 11px 14px -6px rgba(59,144,251,0.1915);
    border-radius: 2px;
    color: #FFF;
    font-size: 16px;
    text-align: center;
    line-height: 50px;
  }
}
.code-pic{
  img{
    display: block;
    // width: 120px;
    height: 36px;
  }
}
</style>